package com.newlink.building.common_base.utils

import android.annotation.SuppressLint
import com.newlink.building.common_base.event.*
import org.greenrobot.eventbus.EventBus
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author Jake
 */
object MqttDoorOperator {

    const val ACTION_OPEN_DOOR = 1
    const val ACTION_CLOSE_DOOR = 0

    const val MSGTYPE_OPEN_DOOR_ACTION: String = "app_open_door_act"
    const val MSGTYPE_OPEN_DOOR_ACK: String = "app_open_door_ack"

    const val MSGTYPE_URGENT_MSG_NOTIFY: String = "app_urgent_msg_notify"
    const val MSGTYPE_URGENT_MSG_NOTIFY_ACK: String = "app_urgent_msg_notify_ack"

    const val MSGTYPE_ALARM_NOTIFY: String = "app_alarm_notify"
    const val MSGTYPE_ALARM_NOTIFY_ACK: String = "app_alarm_notify_ack"

    const val MSGTYPE_ALARM_HANDLE_NOTIFY: String = "app_alarm_handle_notify"
    const val MSGTYPE_ALARM_HANDLE_NOTIFY_ACK: String = "app_alarm_handle_notify_ack"

    // APP 处理安防事件 - Server ACK
    const val MSGTYPE_ALARM_HANDLE: String = "app_alarm_handle"
    const val MSGTYPE_ALARM_HANDLE_ACK: String = "app_alarm_handle_ack"

    // APP 布防模式切换 - Server ACK
    const val MSGTYPE_ARMING_ACT: String = "app_arming_act"
    const val MSGTYPE_ARMING_ACK: String = "app_arming_ack"

    const val MSGTYPE_PUSH: String = "msg_push"
    const val ACK_TIMEOUT: String = "timeout"

    fun sendOpenDoorAction(userId: Long, mac: String, whichDoor: Int, action: Int) {
        EventBus.getDefault().post(
            Base_SendMqttMsgEvent(
                userId,
                createOpenDoorAction(whichDoor, action, mac, "door_${System.currentTimeMillis()}")
            )
        )
        logInfo<MqttDoorOperator>("发送")
    }

    @SuppressLint("SimpleDateFormat")
    private fun createOpenDoorAction(
        whichDoor: Int,
        openOrClose: Int,
        mac: String,
        msgInt: String,

        ): OpenDoorActionBean {

        val sdf = SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ")
        val currentDate = sdf.format(Date())

        val door = OpenDoorActionBean.Data.Door(openOrClose, whichDoor)
        val `data` = OpenDoorActionBean.Data(listOf(door), mac)
        val mqttBean = OpenDoorActionBean(
            currentTime = currentDate,
            msgType = MSGTYPE_OPEN_DOOR_ACTION,
            msgId = msgInt,
            data = `data`
        )

        logDebug<MqttDoorOperator>("准备发送mqtt 开门消息：$mqttBean")
        return mqttBean
    }

}